package zmee.pmct.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import zmee.common.allhelper.OccupiedAmountCalculateHelper;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author yssv
 * @createTime 2022-12-07 10:55
 * @description 支出合同提交校验插件
 */
public class OutContractSubmitExtOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.addAll(this.billEntityType.getAllFields().keySet());
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                ExtendedDataEntity[] dataEntities = this.getDataEntities();
                for (ExtendedDataEntity dataEntity : dataEntities) {
                    totalamountCheck(dataEntity);
                }
            }

            private void totalamountCheck(ExtendedDataEntity extendedDataEntity) {
                DynamicObject dataEntity = extendedDataEntity.getDataEntity();
                 //2、历史已占用金额
                DynamicObject projectDy = dataEntity.getDynamicObject("project");
                if (projectDy == null) {
                    addErrorMessage(extendedDataEntity, "项目为空");
                    return;
                }
                String name = projectDy.getString("name");
                if (!name.contains("公共")) {
                    long projectPk = projectDy.getLong("id");
                    //历史合同，补充协议金额总和
                    BigDecimal totalOccupyAmt = OccupiedAmountCalculateHelper.getAllOccupiedByProject(projectPk);
                    //3、项目立项 项目投资预算总额/项目概算（元）
                    DynamicObject proApprovalDy = BusinessDataServiceHelper.loadSingleFromCache("pmas_pro_approval", "projcetbugamt",
                            new QFilter[]{new QFilter("pro.id", QCP.equals, projectPk)});
                    BigDecimal projcetBugAmt = proApprovalDy.getBigDecimal("projcetbugamt");//项目概算总额
                    //4、判断
                    DynamicObject group = projectDy.getDynamicObject("group");
                    DynamicObject kind = BusinessDataServiceHelper.loadSingle(group.getPkValue(), "bd_projectkind");
                    String parent = kind.getString("parent.number");
                    BigDecimal originalOfTaxAmount;
                    if ("101".equals(parent)) {
                        //含税金额
                        originalOfTaxAmount = dataEntity.getBigDecimal("originaloftaxamount");
                    } else {
                        //不含税金额
                        originalOfTaxAmount = dataEntity.getBigDecimal("originalamount");
                    }

                    //年度项目支出预算总额 + 以前年度历史占用
                    BigDecimal totalAmt = totalOccupyAmt.add(originalOfTaxAmount);

                    if (totalAmt.compareTo(projcetBugAmt) > 0) {
                        this.addErrorMessage(extendedDataEntity, "超过项目概算，请新增项目概算");
                    }
                }
            }
        });
    }
}
